package com.shumi.rbac.config;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Slf4j
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {

    @Autowired
    TokenInterceptor tokenInterceptor;

    @Value("${app.tokenExcludePath}")
    String tokenExcludePath;

    /**
     * token 拦截器
     * */
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns(tokenExcludePath.split(","));
    }

    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/*").allowedOrigins("*").allowedHeaders("Accept, Origin, X-Requested-With, Content-Type, Last-Modified")
            .allowedMethods("GET, POST, HEAD, PUT, DELETE");
    }

}
